package com.alan.chat;

import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

/**
 * 发送消息的线程
 * Created by Alan on 2017/8/5.
 */
public class Send extends Thread {

    private Socket socket;
    private String userName;

    public Send(Socket socket,String userName){
        this.socket = socket;
        this.userName = userName;
    }

    @Override
    public void run() {
        try {
            //创建一个Scanner
            Scanner input = new Scanner(System.in);
            //得到输出流
            OutputStream os = socket.getOutputStream();
            //将字节流转化为字符流
            PrintWriter pw = new PrintWriter(os,true);
            while (true){
                String info = input.nextLine();
                pw.println(userName+"说："+info);
                if(info.endsWith("bye")){
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
